package org.jground.zk.zulX;

import org.jground.zk.util.ZKUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listhead;
import org.zkoss.zul.Listheader;
import org.zkoss.zul.Listitem;

public class ListboxX extends Listbox {

    // private CodeTable codetable;

    // <listbox fixedLayout="true">
    // <listhead sizable="true">
    // <listheader align="center" width="40px"
    // image="/img/Centigrade-Widget-Icons/ArrowsUpDown-16x16.png" />
    // <listheader align="center" width="40px"
    // image="/img/Centigrade-Widget-Icons/Envelope-16x16.png" />
    // <listheader align="center" width="40px"
    // image="/img/Centigrade-Widget-Icons/PaperClip-16x16.png" />
    // <listheader label="Subject" sort="auto"/>
    // <listheader label="Received" sort="auto"/>
    // </listhead>
    // <listitem height="28px">
    // <listcell image="/img/Centigrade-Widget-Icons/ArrowUpOrange-16x16.png" />
    // <listcell image="/img/Centigrade-Widget-Icons/EnvelopeOpen-16x16.png" />
    // <listcell image="/img/Centigrade-Widget-Icons/PaperClip-16x16.png" />
    // <listcell label="ZK Jet 0.8.0 is released" />
    // <listcell label="2008/11/17 17:41:29" />
    // </listitem>

    private Listhead listhead;

    private Listitem currentListItem;

    public ListboxX(String... headers) {
        listhead = new Listhead();
        listhead.setParent(this);
        for (String header : headers) {
            Listheader lh = new Listheader(header);
            lh.setParent(listhead);
        }
    }

    public void addListItem(Listcell... listcells) {
        currentListItem = new Listitem();
        currentListItem.setParent(this);
        for (Listcell listcell : listcells) {
            listcell.setParent(currentListItem);
        }
    }

    public void addComponentItems(Component... components) {
        if (currentListItem == null) {
            currentListItem = new Listitem();
            currentListItem.setParent(this);
        }
        for (Component component : components) {
            Listcell cell = new Listcell();
            component.setParent(cell);
            cell.setParent(currentListItem);
        }
    }

    public void addListItem(String... items) {
        Listcell[] cells = new Listcell[items.length];
        for (int i = 0; i < items.length; i++) {
            cells[i] = new Listcell(items[i]);
        }
        this.addListItem(cells);
    }

    public void removeAllListitems() {
        ZKUtils.removeAllChildren(this);
        this.listhead.setParent(this);
    }

}
